package jannex.sql.extensions;

import jannex.Jannex;
import jannex.internal.ExceptionFactory;
import jannex.sql.JxBlob;

import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.SQLException;

public class BlobImpl implements JxBlob {

    private Blob blob;
    private Jannex jannex;

    public BlobImpl(Blob blob, Jannex jannex) {
        this.blob = blob;
        this.jannex = jannex;
    }

    @Override
    public long length() {
        try {
            return blob.length();
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public byte[] getBytes(long pos, int length) {
        try {
            return blob.getBytes(pos, length);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public InputStream getBinaryStream() {
        try {
            return blob.getBinaryStream();
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public long position(byte[] pattern, long start) {
        try {
            return blob.position(pattern, start);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public long position(Blob pattern, long start) {
        try {
            return blob.position(pattern, start);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public int setBytes(long pos, byte[] bytes) {
        try {
            return blob.setBytes(pos, bytes);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public int setBytes(long pos, byte[] bytes, int offset, int len) {
        try {
            return blob.setBytes(pos, bytes, offset, len);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public OutputStream setBinaryStream(long pos) {
        try {
            return blob.setBinaryStream(pos);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public void truncate(long len) {
        try {
            blob.truncate(len);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public void free() {
        try {
            blob.free();
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public InputStream getBinaryStream(long pos, long length) {
        try {
            return blob.getBinaryStream(pos, length);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }
}
